home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / REPLACE.CPP < prev    next >
Text File  |  1997-05-06  |  1KB  |  55 lines

  1.  #include <algorithm>
  2.  #include <vector>
  3.  #include <iterator>
  4.  
  5.  using namespace std;
  6.  
  7.  bool isOdd(int i)
  8.  {
  9.      return (i%2);
  10.  }
  11.  
  12.  int main ()
  13.  {
  14.    //
  15.    // Initialize a vector with an array of integers.
  16.    //
  17.    int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
  18.    vector<int> v(arr+0, arr+10);
  19.    //
  20.    // Print out original vector.
  21.    //
  22.    cout << "The original list: " << endl << "     ";
  23.    copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
  24.    cout << endl << endl;
  25.    //
  26.    // Replace the number 7 with 11.
  27.    //
  28.    replace(v.begin(), v.end(), 7, 11);
  29.    //
  30.    // Print out vector with 7 replaced.
  31.    //
  32.    cout << "List after replace:" << endl << "     ";
  33.    copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
  34.    cout << endl << endl;
  35.    //
  36.    // Replace 1 & 3 with 13 13 13.
  37.    //
  38.    replace_if(v.begin(), v.begin()+3, isOdd, 13);
  39.    //
  40.    // Print out the remaining vector.
  41.    //
  42.    cout << "List after replace_if:" << endl << "     ";
  43.    copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
  44.    cout << endl << endl;
  45.    //
  46.    // Replace those 13s with 17s on output.
  47.    //
  48.    cout << "List using replace_copy to cout:" << endl << "     ";
  49.    replace_copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "), 13, 17);
  50.  
  51.    cout << endl << endl;
  52.  
  53.    return 0;
  54.  }
  55.